package org.fuys.etp.repo.converter;


import org.fuys.etp.core.model.vo.*;
import org.fuys.etp.repo.po.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-etp
 * @package: org.fuys.etp.repo.converter
 * @className: PoToVoConverter
 * @author: WangJing
 * @description: 持久类转换为视图类用转换器
 * @date: 2025/2/7 0:19
 * @version: 1.0
 */
public class PoToVoConverter {

    public static TpDynamicConfigVO convert(EtpDynamicConfig po) {
        return TpDynamicConfigVO.builder()
                .dynamicVersion(po.getDynamicVersion())
                .maxCoreThreadStep(po.getMaxCoreThreadStep())
                .maxMaxThreadStep(po.getMaxMaxThreadStep())
                .maxQueueSizeStep(po.getMaxQueueSizeStep())
                .windowsChangeTrend(po.getWindowsChangeTrend())
                .pidKp(po.getPidKp())
                .pidKi(po.getPidKi())
                .pidKd(po.getPidKd())
                .pidTarget(po.getPidTarget())
                .baseConfigVersion(po.getBaseConfigVersion())
                .loadLevel(Integer.valueOf(po.getLoadLevel().toString()))
                .windowsSize(po.getWindowSize())
                .windowMinSize(po.getWindowMinSize())
                .windowWeights(Arrays.stream(po.getWindowWeights().split(",")).mapToDouble(Double::parseDouble)
                        .toArray())
                .build();
    }

    public static TpFixedConfigVO convert(EtpFixedConfig po) {
        return TpFixedConfigVO.builder()
                .fixedVersion(po.getFixedVersion())
                .loadRating(Integer.valueOf(po.getLoadRating().toString()))
                .scaleNumType(po.getScaleNumType())
                .scaleCompareType(po.getScaleCompareType())
                .scaleNum(po.getScaleNum())
                .expansionNumType(po.getExpansionNumType())
                .expansionCompareType(po.getExpansionCompareType())
                .expansionNum(po.getExpansionNum())
                .baseConfigVersion(po.getBaseConfigVersion())
                .build();
    }

    public static TpBaseConfigVO convert(EtpBaseConfig po){
        return TpBaseConfigVO.builder()
                .version(po.getVersion())
                .poolCoreSize(po.getPoolCoreSize())
                .poolMaxSize(po.getPoolMaxSize())
                .poolKeepAliveTime(po.getPoolKeepAliveTime())
                .poolAliveTimeUnit(po.getPoolAliveTimeUnit())
                .workQueueSize(po.getWorkQueueSize())
                .selfCheckInterval(po.getSelfCheckInterval())
                .selfCheckIntervalUnit(po.getSelfCheckIntervalUnit().toString())
                .rejectHandleBean(po.getRejectHandleBean())
                .rejectCheckThreshold(po.getRejectCheckThreshold())
                .rejectAlertThreshold(po.getRejectAlertThreshold())
                .cpuPointLimit(po.getCpuPointLimit())
                .memoryPointLimit(po.getMemoryPointLimit())
                .description(po.getDescription())
                .build();
    }

    public static TpRegulationStrategyVO convert(EtpStrategy strategy, List<TpConfigVO> configVOS){
        return TpRegulationStrategyVO.builder()
                .strategyId(strategy.getStrategyId())
                .strategyName(strategy.getStrategyName())
                .strategyDesc(strategy.getStrategyDesc())
                .strategyType(strategy.getStrategyType())
                .alertLevel(strategy.getAlertLevel())
                .loadChain(Arrays.stream(strategy.getLoadChain().split(",")).map(Integer::parseInt)
                        .collect(Collectors.toList()))
                .loadConfigChain(configVOS)
                .startLoadLevel(strategy.getStartLoadLevel())
                .alertType(strategy.getAlertType())
                .checkerChain(strategy.getCheckerChain())
                .metricsChain(strategy.getMetricsChain())
                .alertTo(strategy.getAlertTo())
                .build();
    }

    public static TpConfigHistoryVO convert(EtpConfigHistory history){
        return TpConfigHistoryVO.builder()
                .historyId(history.getHistoryId())
                .poolType(history.getPoolType())
                .poolName(history.getPoolName())
                .previousConfigVersion(history.getPreviousConfigVersion())
                .currentConfigVersion(history.getCurrentConfigVersion())
                .changeTime(history.getChangeTime())
                .changeType(history.getChangeType())
                .changeUserId(history.getChangeUserId())
                .build();
    }
}
